package com.jt.tenant.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.SetPrivacy;
import com.module.share.ShareAction;
import com.jt.tenant.dialog.TipDialog;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

/**
 * Created by AD on 2016/8/17.
 */
public class EquipSettingActivity extends JTActivity implements View.OnClickListener{

    private TextView textViewTitle;

    private TextView textViewBottom;

    private LinearLayout equip_control, equip_wifi ,equip_shock,equip_gprs,equip_fence;

    private ImageView iv_equip_control, iv_equip_wifi ,iv_equip_permission,iv_equip_shock,iv_equip_gprs,iv_equip_fence;

    private ImageView equip_permission;

    private boolean flag = false;

    public static RefreshLinster refreshLinster;

    private SetPrivacy setPrivacy;

    private String privacy;

    private String message;

    private ShareAction mShare;

    private String share_url = "http://jtbox.com/BCB/html/index.html";

    private String share_title = "优路兔老用户推荐有惊喜";

    private String share_content = "优路兔老用户推荐有惊喜";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_setting);

        initView();
    }

    private void initView(){

        textViewTitle = (TextView) findViewById(R.id.tv_title_name);

        textViewTitle.setText("设备管理");

        (equip_control = (LinearLayout)findViewById(R.id.lin_equip_control)).setOnClickListener(this);

        (equip_wifi = (LinearLayout) findViewById(R.id.lin_equip_wifi)).setOnClickListener(this);

        (equip_permission = (ImageView) findViewById(R.id.privacy_setting)).setOnClickListener(this);

        (equip_shock = (LinearLayout)findViewById(R.id.lin_equip_shock)).setOnClickListener(this);

        (equip_gprs = (LinearLayout)findViewById(R.id.lin_equip_gprs)).setOnClickListener(this);

        (equip_fence = (LinearLayout)findViewById(R.id.lin_equip_fence)).setOnClickListener(this);

        iv_equip_control = (ImageView) findViewById(R.id.iv_equip_control);

        iv_equip_wifi = (ImageView) findViewById(R.id.iv_equip_wifi);

        iv_equip_permission = (ImageView) findViewById(R.id.iv_equip_permission);

        iv_equip_shock = (ImageView) findViewById(R.id.iv_equip_shock);

        iv_equip_gprs = (ImageView) findViewById(R.id.iv_equip_gprs);

        iv_equip_fence = (ImageView) findViewById(R.id.iv_equip_fence);

        if(!JTApplication.jtPreferences.readAccountState().equals("2")){

           iv_equip_wifi.setImageResource(R.mipmap.icon_equip_wifi_off);

            iv_equip_permission.setImageResource(R.mipmap.icon_equip_permission_off);

            iv_equip_shock.setImageResource(R.mipmap.icon_equip_shock_off);

            iv_equip_gprs.setImageResource(R.mipmap.icon_equip_gprs_off);

            iv_equip_fence.setImageResource(R.mipmap.icon_equip_fence_off);

        }

        flag = JTApplication.jtPreferences.readPrivacy();

        if(flag){

            equip_permission.setImageResource(R.mipmap.switch_on);

        }

        setPrivacy = new SetPrivacy(new AsyCallBack<SetPrivacy.Info>(){

            @Override
            public void onSuccess(String toast, int type, SetPrivacy.Info info) throws Exception {

                super.onSuccess(toast, type, info);

                if(info.msg.equals("000")){

                    if(info.gpsType.equals("0")){

                        JTApplication.jtPreferences.savePrivacy(false);

                        equip_permission.setImageResource(R.mipmap.switch_off);

                        UtilToast.show(EquipSettingActivity.this,"隐私保护已关闭");

                    }else {

                        JTApplication.jtPreferences.savePrivacy(true);

                        equip_permission.setImageResource(R.mipmap.switch_on);

                        UtilToast.show(EquipSettingActivity.this,"隐私保护已开启");

                    }

                    flag = !flag;


                }else {

                    UtilToast.show(EquipSettingActivity.this,info.msg);

                }


            }

            @Override
            public void onFail(String toast, int type) throws Exception {

                super.onFail(toast, type);

                UtilToast.show(EquipSettingActivity.this,toast);

            }
        });

       refreshLinster = new RefreshLinster() {
           @Override
           public void refresh() {

                setPrivacy.gpsType = privacy;

               setPrivacy.execute(EquipSettingActivity.this);

           }
       };
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.lin_equip_control:

//                绑定设备
                startActivity(new Intent(this, EquipInfoActivity.class));

                break;

            case R.id.lin_equip_wifi:

                if(JTApplication.jtPreferences.readAccountState().equals("2")) {

                    startActivity(new Intent(this, WifiControlActivity.class));

                }else {

                    UtilToast.show(this, "该功能需要绑定OBD设备");

                }

                break;

//            震动设置
            case R.id.lin_equip_shock:

                if(JTApplication.jtPreferences.readAccountState().equals("2")) {

                    startActivity(new Intent(this, ShockSettingActivity.class));

                }else {

                    UtilToast.show(this, "该功能需要绑定OBD设备");

                }



                break;

            case R.id.lin_equip_gprs:

                if(JTApplication.jtPreferences.readAccountState().equals("2")) {

                    startActivity(new Intent(this, DataRechargeActivity.class));

                }else {

                    UtilToast.show(this, "该功能需要绑定OBD设备");

                }

                break;

            case R.id.lin_equip_fence:

                if(JTApplication.jtPreferences.readAccountState().equals("2")) {

                    startActivity(new Intent(this, FenceListActivity.class));

                }else {

                    UtilToast.show(this, "该功能需要绑定OBD设备");

                }

                break;

            case R.id.privacy_setting:

                if(JTApplication.jtPreferences.readAccountState().equals("2")) {

                    if(flag){
//                    0：显示定位轨迹，1：不显示   true 不显示，false 显示

                        privacy = "0";

                        message = "关闭后可查看详细定位、行程等信息，是否确认关闭？";

                    }else {

                        privacy = "1";

                        message = "开启后不可查看详细定位、行程等信息，是否确认开启？";


                    }

                    TipDialog privacyDialog = new TipDialog(EquipSettingActivity.this);

                    privacyDialog.setTip(message);

                    privacyDialog.setType(6);

                    privacyDialog.show();

                }else {

                    UtilToast.show(this, "该功能需要绑定OBD设备");

                }

                break;
        }
    }

    public abstract class RefreshLinster {

        public abstract void refresh();

    }


}
